<?php

namespace addons\official\controller\api;

use addons\official\model\OfficialCategory;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;

/**
 * 栏目接口
 */
class Category extends BaseApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    /**
     * 获取所有栏目数据
     * @ApiMethod   (POST)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询成功",
        "time": "1634823263",
        "data": [
            {
                "id": "ID",
                "name": "栏目名称",
                "e_name": "英文名称",
                "pid": "父级ID",
                "status": "类型:1=单页类型,2=列表类型,3=外链类型",
                "url": "跳转路径<外链模式有效>",
                "son": [
                    {
                        "id": "ID",
                        "name": "栏目名称",
                        "e_name": "英文名称",
                        "pid": "父级ID",
                        "status": "类型:1=单页类型,2=列表类型,3=外链类型",
                        "url": "跳转路径<外链模式有效>"
                    }
                ]
            }
        ]
     })
     */
    public function getAllCategory(){
        //生成配置文件
        $official_tree_category_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_tree_category.php';
        if (!file_exists($official_tree_category_url)) {
            $official_category = (new OfficialCategory())
                ->with(['son'])
                ->where('pid', 0)
                ->field(['content', 'e_content'], true)
                ->where('deletetime', null)
                ->order('weigh', 'desc')
                ->select();
            $tree_category_all = [];
            foreach ($official_category as $item) {
                $tree_category_all[] = $item->toArray();
            }
            file_put_contents(
                $official_tree_category_url,
                '<?php' . "\n\nreturn " . var_export_short($tree_category_all) . ";\n"
            );
        } else {
            $tree_category_all = include $official_tree_category_url;
        }
        $array = array();
        //整理数据
        foreach ($tree_category_all as $item) {
            if ($item['is_nav_show']){
                $data = [
                    "id" => $item['id'],
                    "name" => $item['name'],
                    "e_name" => $item['e_name'],
                    "image"=>cdnurl($item['image'],true),
                    "pid"=>$item['pid'],
                    "status"=>$item['status'],
                    "url"=>$item['url'],
                    "son"=>[],
                    "official_rule_id"=>$item['official_rule_id']
                ];
                if ($data['official_rule_id']){
                    $data["official_rule"] = db('official_rule')->where('id',$data['official_rule_id'])->find();
                }
                if (!empty($item['son']) && count($item['son'])){
                    foreach ($item['son'] as $value){
                        if ($value['is_nav_show']){
                            $arr = [
                                "id" => $value['id'],
                                "name" => $value['name'],
                                "e_name" => $value['e_name'],
                                "image"=>cdnurl($value['image'],true),
                                "pid"=>$value['pid'],
                                "status"=>$value['status'],
                                "url"=>$value['url'],
                                "official_rule_id"=>$value['official_rule_id']
                            ];
                            if ($arr['official_rule_id']){
                                $arr["official_rule"] = db('official_rule')->where('id',$arr['official_rule_id'])->find();
                            }
                            $data['son'][] = $arr;
                        }
                    }
                }
                $array[] = $data;
            }
        }
        $this->success(__api('','The query is successful'), $array);
    }



    /**
     * 获取单条栏目数据
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="integer", required=false, description="栏目id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询成功",
        "time": "1634823263",
        "data": [
            {
                "id": "ID",
                "name": "栏目名称",
                "e_name": "英文名称",
                "pid": "父级ID",
                "status": "类型:1=单页类型,2=列表类型,3=外链类型",
                "url": "跳转路径<外链模式有效>",
            }
        ]
     })
     */
    public function getOneCategory(){
        $id = (int) input('param.id');
        if (!$id){
            $this->error(__api('','Invalid parameters'));
        }
        $category = (new OfficialCategory())
            ->where('id', $id)
            ->where('deletetime', null)
            ->find();
        $all_data = (new OfficialCategory());
        if ($category['pid']){
            $all_data = $all_data->where('id', $category['pid']);
        }else{
            $all_data = $all_data->where('id', $category['id']);
        }
        $all_data = $all_data->where('deletetime', null)
            ->find();
        //添加规则
        if ($category['official_rule_id']){
            $category["official_rule"] = db('official_rule')->where('id',$category['official_rule_id'])->find();
        }
        //查询子级
        $son = (new OfficialCategory())
            ->where('pid', $all_data['id'])
            ->field(['content', 'e_content'], true)
            ->where('deletetime', null)
            ->where('is_nav_show',1)
            ->order('weigh', 'desc')
            ->select();
        $all_son = array();
        if (!empty($son)){
            foreach ($son as $key => $item){
                //查询子级规则
                if ($item['official_rule_id']){
                    $item["official_rule"] = db('official_rule')->where('id',$item['official_rule_id'])->find();
                }
                $all_son[] = $item;
            }
        }
        $all_data['son'] = $all_son;

        $this->success(__api('','The query is successful'), ['category'=>$category,'all_data'=>$all_data]);
    }





}
